#include "stdafx.h"
#include "Resource.h"

#include "../RMSLib/VC70SocketThread.h"
#include "../RMSLib/RMSWaveClientSocket.h"
#include "../RMSLib/VC70ClientSession.h"
#include "../RMSLib/VideoPlayer.h"


CVC70Session::CVC70Session()
{
	m_camera.m_bActive = false;
}

CVC70Session::~CVC70Session()
{
	Close();
}

void CVC70Session::setClient(ClientInterface* pclient)
{
	ClientAccess::setClient(pclient);
	if(pclient == NULL)
	{
		m_socket.setClient(NULL);
#ifdef VC70
		if(m_socket.m_bStored)
		{
			CVideoPlayer* pvp = getPlayer();
			if(pvp) pvp->setClient(NULL);
		}
#endif //def VC70
	}
}

void CVC70Session::Init()
{
	m_camera.m_bActive = false;

	m_socket.setClient(getClient());
	m_socket.m_nCamera = 0;
}

void CVC70Session::SetCameraInfo(CInfo& info)
{
	m_camera.m_ip = info.m_ip;
	m_camera.m_nPort = info.m_port;
	strcpy(m_camera.m_szTime,"");
	strcpy(m_camera.m_szDate,"");

	m_socket.m_info = info;
	m_socket.m_nCurrentFrameRate = info.m_iFrameRate;
}

void CVC70Session::SetFrameRate(int nFrameRate)
{
	m_socket.m_info.m_iFrameRate = nFrameRate;
	m_socket.m_nCurrentFrameRate = nFrameRate;
}

int CVC70Session::GetCurrentFrameRate()
{
	return m_socket.m_nCurrentFrameRate;
}

int CVC70Session::GetFrameRate()
{
	return m_socket.m_info.m_iFrameRate;
}

int CVC70Session::GetBirate()
{
#ifdef VC70
	if( m_socket.m_bStored )
	{
		CVideoPlayer* pvp = getPlayer();
		return (pvp) ? pvp->m_nBitrate : 0;
	}
	else
#endif //def VC70
		return m_socket.m_nBitrate;
}

int CVC70Session::Connect(BOOL bStored)
{
	if (m_camera.m_bActive) Disconnect();// need to disconnect first!
	m_socket.m_bStored = bStored;
	m_socket.Execute();
	m_camera.m_bActive = true;
	return 0;
}

int CVC70Session::Disconnect()
{
	if(!m_camera.m_bActive)
		return 0;

//	enterCritical();
	if(m_camera.m_bActive)
	{
//		m_waveSocket.SetState(CRMSWaveClientSocket::STATE_STOP);
		m_waveSocket.SetState(CRMSWaveClientSocket::STATE_EXIT);
#ifdef _JNC_REC_
		m_socket.CloseSocket(!m_socket.m_bGetRecords);
#else //def _JNC_REC_
		m_socket.CloseSocket();
#endif //def _JNC_REC_
		m_socket.Terminate(FALSE);
		m_camera.m_bActive = false;
	}
//	leaveCritical();
	return 0;
}

void CVC70Session::SetPriority()
{
	HANDLE thread=m_socket.m_hThread;
	if( thread != NULL )
	{
		SetThreadPriority(thread,THREAD_PRIORITY_ABOVE_NORMAL);
	}
}


void CVC70Session::Close()
{
	Disconnect();
}

void CVC70Session::SetTimeMark(const TimeMark &timeStart, const TimeMark &timeEnd)
{
	m_socket.m_timeStart = timeStart;
	m_socket.m_timeEnd = timeEnd;
}

#ifdef _JNC_REC_
void CVC70Session::SetTimeMark1(const TimeMark &timeFrom, const TimeMark &timeTo)
{
	m_socket.m_timeFrom = timeFrom;
	m_socket.m_timeTo = timeTo;
}

void CVC70Session::SetGetListFlag(BOOL flag)
{
	m_socket.m_bGetRecords = flag;
}
#endif //def _JNC_REC_

int CVC70Session::GetMarker()
{
#ifdef VC70
	CVideoPlayer* pvp = getPlayer();
	if(pvp && pvp->m_lTotalStoredData)
	{
		return ((double)((pvp->m_lCurrentStoredData) * 100.0) /
				(double)pvp->m_lTotalStoredData);
	}
#endif //def VC70
	return 0;
}

int CVC70Session::GetPosSlider()
{
#ifdef VC70
	CVideoPlayer* pvp = getPlayer();
	if(pvp && pvp->m_lTotalStoredData)
	{
		return ((double)((pvp->m_lCurrentPlayData) * 100.0) /
				(double)pvp->m_lTotalStoredData);
	}
#endif //def VC70
	return 0;
}
